/**
 *  Copyright (c) 1997-2013, www.tinygroup.org (luo_guo@icloud.com).
 *
 *  Licensed under the GPL, Version 3.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.gnu.org/licenses/gpl.html
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.tinygroup.dbf.impl;

import org.tinygroup.dbf.DbfDataSet;
import org.tinygroup.dbf.DbfDocument;
import org.tinygroup.dbf.DbfReader;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;

/**
 * 数据DBF数据文件读入内存
 * Created by luoguo on 2014/7/10.
 */
public class MemoryDbfReader implements DbfReader {
    /**
     * 用指定编码读入DBF文件
     *
     * @param fileName 要读入的文件名
     * @param encode   要采用的编码
     * @return
     * @throws IOException
     * @throws ParseException
     */
    public DbfDocument readDbf(String fileName, String encode) throws IOException, ParseException {
        return readDbf(new File(fileName), encode);
    }

    /**
     * 用指定编码读入DBF文件
     *
     * @param file   要读入的文件
     * @param encode 要采用的编码
     * @return
     * @throws IOException
     * @throws ParseException
     */
    public DbfDocument readDbf(File file, String encode) throws IOException, ParseException {
        StreamDbfReader streamDbfReader = new StreamDbfReader();
        DbfDocument dbfDocument = streamDbfReader.readDbf(file, encode);
        MemoryDbfDataSet memoryDbfDataSet = new MemoryDbfDataSet();
        DbfDataSet dataSet = dbfDocument.getDataSet();
        while (dataSet.next()) {
            memoryDbfDataSet.getDbfRecords().add(dataSet.getDbfRecord());
        }
        streamDbfReader.close();
        dbfDocument.setDataSet(memoryDbfDataSet);
        return dbfDocument;
    }

    /**
     * 关闭Reader
     *
     * @throws IOException
     */
    public void close() throws IOException {
        //由于是直接读取存，所以关闭时什么也不用做
    }
}
